/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = ora_ttc8_tti_uds.c                                 */
/*  NOTE      =                                                    */
/*  DATE      = 2014/10/23 by songbb                               */
/*******************************************************************/

#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>

#include "frcomm.h"
#include "frstr.h"
#include "frmem.h"
#include "frlog.h"

#include "conntrack.h"
#include "frct_comm.h"

#include "../../ora_core.h"
#include "../../comm/ora_comm.h"
#include "../../comm/ora_util.h"
#include "../ora_mar_engine.h"
#include "ora_ttc8_tti_oac.h"
#include "ora_ttc8_tti_uds.h"
#include "../../comm/ora_charset.h"

#define APM_ORA_TTC8_UDS_ANALYZE_MORE_BY 2

/*******************************************************************************
*  FUNC     :  长连接过程中分析uds第2个开始的部分
*  ARGS     :  *p_st_conn                           (ct信息)
*           :  *p_st_sess                           (Oracle私有的session)
*           :  *p_st_ora_packet                     (Oracle数据包)
*           :  i_index                              (当前uds的序号)
*  RTN      :  0                                    (FR_SUCC)
*              1                                    (FR_FAIL)
*  NOTE     :
*******************************************************************************/
FR_STATIC int __ora_ttc8_tti_uds_analyze_more_jdbc(Conntrack *p_st_conn
                                                    , L7session_ora *p_st_sess
                                                    , L7_ora_packet *p_st_ora_packet
                                                    , int i_index)
{
    ORA_PRINT_INFO("enter __ora_ttc8_tti_uds_analyze_more_jdbc\n");
    short s_tmp = 0;
    short s_udskpos = 0;
    int i_tmp = 0;

    if (ora_ttc8_tti_oac_analyze(p_st_conn, p_st_sess, p_st_ora_packet, i_index) != FR_SUCC)
        goto error;

    if (ora_mar_unmarshalUB1(p_st_ora_packet, &s_tmp) != FR_SUCC) // i
        goto error;

    if (ora_mar_unmarshalUB1(p_st_ora_packet, &s_tmp) != FR_SUCC) // udscnl
        goto error;

    if (ora_mar_skipDALC(p_st_sess, p_st_ora_packet) != FR_SUCC) // udscolnm
        goto error;

    if (ora_mar_skipDALC(p_st_sess, p_st_ora_packet) != FR_SUCC) // udssnm
        goto error;

    if (ora_mar_skipDALC(p_st_sess, p_st_ora_packet) != FR_SUCC) // udstnm
        goto error;

    if (p_st_sess->i_ttc8_stmt_resp_udskpos == FR_YES)
    {
        if (ora_mar_unmarshalSB2(p_st_sess, p_st_ora_packet, &s_udskpos) != FR_SUCC) // udskpos
            goto error;
        if (s_udskpos != i_index)
        {
            ORA_PRINT_ALERT("s_udskpos:[%d] != i_index:[%d]\n", s_udskpos, i_index);
            goto error;
        }
        if (p_st_sess->i_ttc8_stmt_resp_udsflg == FR_YES)
        {
            if (ora_mar_unmarshalSB4(p_st_sess, p_st_ora_packet, &i_tmp) != FR_SUCC) // udsflg
                goto error;
        }
    }

    return FR_SUCC;
error:
    return FR_FAIL;
}

/*******************************************************************************
*  FUNC     :  长连接过程中分析uds部分
*  ARGS     :  *p_st_conn                           (ct信息)
*           :  *p_st_sess                           (Oracle私有的session)
*           :  *p_st_ora_packet                     (Oracle数据包)
*           :  i_index                              (当前uds的序号)
*  RTN      :  0                                    (FR_SUCC)
*              1                                    (FR_FAIL)
*  NOTE     :
*******************************************************************************/
FR_STATIC int __ora_ttc8_tti_uds_analyze_jdbc(Conntrack *p_st_conn
                                                , L7session_ora *p_st_sess
                                                , L7_ora_packet *p_st_ora_packet
                                                , int i_index)
{
    ORA_PRINT_INFO("enter __ora_ttc8_tti_uds_analyze_jdbc\n");
    int i_tmp = 0;
    int i_loop = 0;
    int i_start_position = bytebuff_getPosition(p_st_ora_packet->p_st_packet);

analyze:
    if (p_st_sess->i_ttc8_stmt_resp_udsflg == APM_ORA_FLAG_UNSET)
        p_st_sess->i_ttc8_stmt_resp_udsflg = FR_NO;
    else if (p_st_sess->i_ttc8_stmt_resp_udsflg == FR_NO)
        p_st_sess->i_ttc8_stmt_resp_udsflg = FR_YES;
    else
    {
        p_st_sess->i_ttc8_stmt_resp_udsflg = APM_ORA_FLAG_UNSET;
        goto error;
    }
    if (bytebuff_setPosition(p_st_ora_packet->p_st_packet, i_start_position) != FR_SUCC)
        goto error;

    ORA_PRINT_ALERT("__ora_ttc8_tti_uds_analyze_jdbc set i_ttc8_stmt_resp_udsflg = %s\n",
                    p_st_sess->i_ttc8_stmt_resp_udsflg == APM_ORA_FLAG_UNSET ? "UNSET" : (p_st_sess->i_ttc8_stmt_resp_udsflg == FR_YES ? "FR_YES" : "FR_NO"));

    if (p_st_sess->i_ttc8_stmt_resp_udsflg == FR_YES)
    {
        if (ora_mar_unmarshalSB4(p_st_sess, p_st_ora_packet, &i_tmp) != FR_SUCC) // udsflg
            goto analyze;
    }

    for (i_loop = i_index + 1; i_loop < APM_ORA_TTC8_UDS_ANALYZE_MORE_BY + 1; i_loop++)
    {
        if (__ora_ttc8_tti_uds_analyze_more_jdbc(p_st_conn, p_st_sess, p_st_ora_packet, i_loop) != FR_SUCC)
            goto analyze;
    }

    return FR_SUCC;
error:
    p_st_ora_packet->fragment_state = APM_ORA_FRAGMENT_INIT;
    if (bytebuff_setPosition(p_st_ora_packet->p_st_packet, i_start_position) != FR_SUCC)
        goto error;
    return FR_FAIL;
}

/*******************************************************************************
*  FUNC     :  对应于 void unmarshal() 方法
*  ARGS     :  *p_st_conn                           (ct信息)
*           :  *p_st_sess                           (Oracle私有的session)
*           :  *p_st_ora_packet                     (Oracle数据包)
*           :  *p_st_stmt                           (statement)
*           :  i_index                              (当前uds的序号)
*           :  i_numuds                             (总共有多少个uds)
*  RTN      :  0                                    (FR_SUCC)
*              1                                    (FR_FAIL)
*  NOTE     :
*******************************************************************************/
FR_STATIC int __ora_ttc8_tti_uds_analyze_oci_oneuds(Conntrack *p_st_conn
                                                    , L7session_ora *p_st_sess
                                                    , L7_ora_packet *p_st_ora_packet
                                                    , int i_index
                                                    , int *p_i_udskpos_chk)
{
    ORA_PRINT_INFO("enter __ora_ttc8_tti_uds_analyze_oci_oneuds\n");
    short s_tmp = 0;
    int i_tmp = 0;
    int i_tmp_len = 0;
    char *p_tmp = NULL;

    if (ora_ttc8_tti_oac_analyze(p_st_conn, p_st_sess, p_st_ora_packet, i_index) != FR_SUCC)
        goto error;

    if (ora_mar_unmarshalUB1(p_st_ora_packet, &s_tmp) != FR_SUCC) // i
        goto error;
    if (ora_mar_unmarshalUB1(p_st_ora_packet, &s_tmp) != FR_SUCC) // udscnl
        goto error;
    if (ora_mar_unmarshalDALC(p_st_sess, p_st_ora_packet, &p_tmp, &i_tmp_len) == FR_SUCC) // udscolnm
        ora_free((void **) &p_tmp);
    else
        goto error;
    if (ora_mar_skipDALC(p_st_sess, p_st_ora_packet) != FR_SUCC) // udssnm
        goto error;
    if (ora_mar_skipDALC(p_st_sess, p_st_ora_packet) != FR_SUCC) // udstnm
        goto error;

    if (p_st_sess->i_ttc8_stmt_resp_udskpos == FR_YES)
    {
        if (ora_mar_unmarshalSB2(p_st_sess, p_st_ora_packet, &s_tmp) != FR_SUCC) // udskpos
            goto error;
        if (*p_i_udskpos_chk == -1)
            *p_i_udskpos_chk = s_tmp;
        else
            *p_i_udskpos_chk += 1;

        if (s_tmp != *p_i_udskpos_chk)
        {
            ORA_PRINT_ALERT("s_udskpos:[%d] != *p_i_udskpos_chk:[%d]\n", s_tmp, *p_i_udskpos_chk);
            goto error;
        }
        if (p_st_sess->i_ttc8_stmt_resp_udsflg == FR_YES)
        {
            if (ora_mar_unmarshalSB4(p_st_sess, p_st_ora_packet, &i_tmp) != FR_SUCC) // udsflg
                goto error;
        }
    }

    ORA_PRINT_INFO("leave __ora_ttc8_tti_uds_analyze_oci_oneuds\n");
    return FR_SUCC;
error:
    return FR_FAIL;
}

/*******************************************************************************
*  FUNC     :  对应于 void unmarshal() 方法
*  ARGS     :  *p_st_conn                           (ct信息)
*           :  *p_st_sess                           (Oracle私有的session)
*           :  *p_st_ora_packet                     (Oracle数据包)
*           :  *p_st_stmt                           (statement)
*           :  i_index                              (当前uds的序号)
*           :  i_numuds                             (总共有多少个uds)
*  RTN      :  0                                    (FR_SUCC)
*              1                                    (FR_FAIL)
*  NOTE     :
*******************************************************************************/
int ora_ttc8_tti_uds_analyze_oci(Conntrack *p_st_conn
                                , L7session_ora *p_st_sess
                                , L7_ora_packet *p_st_ora_packet
                                , int i_numuds)
{
    ORA_PRINT_INFO("enter ora_ttc8_tti_uds_analyze_oci\n");
    int i_loop;
    long long ll_tmp = 0LL;
    int i_ttc8_stmt_resp_oac_skip1_old = p_st_sess->i_ttc8_stmt_resp_oac_skip1;
    int i_ttc8_stmt_resp_oac_skipaftervsn_old = p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn;
    int i_ttc8_stmt_resp_udskpos_old = p_st_sess->i_ttc8_stmt_resp_udskpos;
    int i_ttc8_stmt_resp_udsflg_old = p_st_sess->i_ttc8_stmt_resp_udsflg;
    int i_udskpos_chk = -1;

    int i_pos = bytebuff_getPosition(p_st_ora_packet->p_st_packet);
analyze:
    /**
     * 组合：
     * 1. FR_YES   FR_NO   FR_YES  FR_NO
     * 2. FR_YES   FR_NO   FR_NO   FR_NO
     * 3. FR_YES   FR_YES  FR_YES  FR_YES
     * 4. FR_YES   FR_YES  FR_YES  FR_NO
     * 5. FR_NO    FR_NO   FR_YES  FR_NO
     * 6. FR_NO    FR_NO   FR_NO   FR_NO
     * 7. FR_YES   FR_NO   FR_YES  FR_YES
     * 8. FR_YES   FR_YES  FR_NO   FR_NO
     * 9. FR_NO    FR_NO   FR_YES  FR_YES
     */
    if (p_st_sess->i_ttc8_stmt_resp_oac_skip1 == APM_ORA_FLAG_UNSET ||
        p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn == APM_ORA_FLAG_UNSET ||
        p_st_sess->i_ttc8_stmt_resp_udskpos == APM_ORA_FLAG_UNSET ||
        p_st_sess->i_ttc8_stmt_resp_udsflg == APM_ORA_FLAG_UNSET)
    {
        // 1. FR_YES   FR_NO   FR_YES  FR_NO
        p_st_sess->i_ttc8_stmt_resp_oac_skip1 = FR_YES;
        p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn = FR_NO;
        p_st_sess->i_ttc8_stmt_resp_udskpos = FR_YES;
        p_st_sess->i_ttc8_stmt_resp_udsflg = FR_NO;
    }
    else if (p_st_sess->i_ttc8_stmt_resp_oac_skip1 == FR_YES &&
             p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn == FR_NO &&
             p_st_sess->i_ttc8_stmt_resp_udskpos == FR_YES &&
             p_st_sess->i_ttc8_stmt_resp_udsflg == FR_NO)
    {
        // 2. FR_YES   FR_NO   FR_NO   FR_NO
        p_st_sess->i_ttc8_stmt_resp_oac_skip1 = FR_YES;
        p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn = FR_NO;
        p_st_sess->i_ttc8_stmt_resp_udskpos = FR_NO;
        p_st_sess->i_ttc8_stmt_resp_udsflg = FR_NO;
    }
    else if (p_st_sess->i_ttc8_stmt_resp_oac_skip1 == FR_YES &&
             p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn == FR_NO &&
             p_st_sess->i_ttc8_stmt_resp_udskpos == FR_NO &&
             p_st_sess->i_ttc8_stmt_resp_udsflg == FR_NO)
    {
        // 3. FR_YES   FR_YES  FR_YES  FR_YES
        p_st_sess->i_ttc8_stmt_resp_oac_skip1 = FR_YES;
        p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn = FR_YES;
        p_st_sess->i_ttc8_stmt_resp_udskpos = FR_YES;
        p_st_sess->i_ttc8_stmt_resp_udsflg = FR_YES;
    }
    else if (p_st_sess->i_ttc8_stmt_resp_oac_skip1 == FR_YES &&
             p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn == FR_YES &&
             p_st_sess->i_ttc8_stmt_resp_udskpos == FR_YES &&
             p_st_sess->i_ttc8_stmt_resp_udsflg == FR_YES)
    {
        // 4. FR_YES   FR_YES  FR_YES  FR_NO
        p_st_sess->i_ttc8_stmt_resp_oac_skip1 = FR_YES;
        p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn = FR_YES;
        p_st_sess->i_ttc8_stmt_resp_udskpos = FR_YES;
        p_st_sess->i_ttc8_stmt_resp_udsflg = FR_NO;
    }
    else if (p_st_sess->i_ttc8_stmt_resp_oac_skip1 == FR_YES &&
             p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn == FR_YES &&
             p_st_sess->i_ttc8_stmt_resp_udskpos == FR_YES &&
             p_st_sess->i_ttc8_stmt_resp_udsflg == FR_NO)
    {
        // 5. FR_NO    FR_NO   FR_YES  FR_NO
        p_st_sess->i_ttc8_stmt_resp_oac_skip1 = FR_NO;
        p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn = FR_NO;
        p_st_sess->i_ttc8_stmt_resp_udskpos = FR_YES;
        p_st_sess->i_ttc8_stmt_resp_udsflg = FR_NO;
    }
    else if (p_st_sess->i_ttc8_stmt_resp_oac_skip1 == FR_NO &&
             p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn == FR_NO &&
             p_st_sess->i_ttc8_stmt_resp_udskpos == FR_YES &&
             p_st_sess->i_ttc8_stmt_resp_udsflg == FR_NO)
    {
        // 6. FR_NO    FR_NO   FR_NO   FR_NO
        p_st_sess->i_ttc8_stmt_resp_oac_skip1 = FR_NO;
        p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn = FR_NO;
        p_st_sess->i_ttc8_stmt_resp_udskpos = FR_NO;
        p_st_sess->i_ttc8_stmt_resp_udsflg = FR_NO;
    }
    else if (p_st_sess->i_ttc8_stmt_resp_oac_skip1 == FR_NO &&
             p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn == FR_NO &&
             p_st_sess->i_ttc8_stmt_resp_udskpos == FR_NO &&
             p_st_sess->i_ttc8_stmt_resp_udsflg == FR_NO)
    {
        // 7. FR_YES   FR_NO   FR_YES  FR_YES
        p_st_sess->i_ttc8_stmt_resp_oac_skip1 = FR_YES;
        p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn = FR_NO;
        p_st_sess->i_ttc8_stmt_resp_udskpos = FR_YES;
        p_st_sess->i_ttc8_stmt_resp_udsflg = FR_YES;
    }
    else if (p_st_sess->i_ttc8_stmt_resp_oac_skip1 == FR_YES &&
             p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn == FR_NO &&
             p_st_sess->i_ttc8_stmt_resp_udskpos == FR_YES &&
             p_st_sess->i_ttc8_stmt_resp_udsflg == FR_YES)
    {
        // 8. FR_YES   FR_YES  FR_NO   FR_NO
        p_st_sess->i_ttc8_stmt_resp_oac_skip1 = FR_YES;
        p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn = FR_YES;
        p_st_sess->i_ttc8_stmt_resp_udskpos = FR_NO;
        p_st_sess->i_ttc8_stmt_resp_udsflg = FR_NO;
    }
    else if (p_st_sess->i_ttc8_stmt_resp_oac_skip1 == FR_YES &&
             p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn == FR_YES &&
             p_st_sess->i_ttc8_stmt_resp_udskpos == FR_NO &&
             p_st_sess->i_ttc8_stmt_resp_udsflg == FR_NO)
    {
        // 9. FR_NO    FR_NO   FR_YES  FR_YES
        p_st_sess->i_ttc8_stmt_resp_oac_skip1 = FR_NO;
        p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn = FR_NO;
        p_st_sess->i_ttc8_stmt_resp_udskpos = FR_YES;
        p_st_sess->i_ttc8_stmt_resp_udsflg = FR_YES;
    }
    else
        goto error;

    if (bytebuff_setPosition(p_st_ora_packet->p_st_packet, i_pos) != FR_SUCC)
        goto error;

    i_udskpos_chk = -1;
    for (i_loop = 0; i_loop < i_numuds; i_loop++)
    {
        if (__ora_ttc8_tti_uds_analyze_oci_oneuds(p_st_conn, p_st_sess, p_st_ora_packet, i_loop, &i_udskpos_chk) != FR_SUCC)
            goto analyze;
    }

    // 校验随后的arrayOfByte1值，arrayOfByte1以DALC编码，这里只校验开头的长度是不是7
    if (ora_mar_unmarshalUB4(p_st_sess, p_st_ora_packet, &ll_tmp) != FR_SUCC || ll_tmp != 7LL)
        goto analyze;

    ORA_PRINT_ALERT("set i_ttc8_stmt_resp_oac_skip1 = %s, i_ttc8_stmt_resp_oac_skipaftervsn = %s, i_ttc8_stmt_resp_udskpos = %s, i_ttc8_stmt_resp_udsflg = %s\n",
                    p_st_sess->i_ttc8_stmt_resp_oac_skip1 == APM_ORA_FLAG_UNSET ? "UNSET" : (p_st_sess->i_ttc8_stmt_resp_oac_skip1 == FR_YES ? "FR_YES" : "FR_NO"),
                    p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn == APM_ORA_FLAG_UNSET ? "UNSET" : (p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn == FR_YES ? "FR_YES" : "FR_NO"),
                    p_st_sess->i_ttc8_stmt_resp_udskpos == APM_ORA_FLAG_UNSET ? "UNSET" : (p_st_sess->i_ttc8_stmt_resp_udskpos == FR_YES ? "FR_YES" : "FR_NO"),
                    p_st_sess->i_ttc8_stmt_resp_udsflg == APM_ORA_FLAG_UNSET ? "UNSET" : (p_st_sess->i_ttc8_stmt_resp_udsflg == FR_YES ? "FR_YES" : "FR_NO"));

    ORA_PRINT_INFO("leave ora_ttc8_tti_uds_analyze_oci\n");
    return FR_SUCC;
error:
    p_st_ora_packet->fragment_state = APM_ORA_FRAGMENT_INIT;
    p_st_sess->i_ttc8_stmt_resp_oac_skip1 = i_ttc8_stmt_resp_oac_skip1_old;
    p_st_sess->i_ttc8_stmt_resp_oac_skipaftervsn = i_ttc8_stmt_resp_oac_skipaftervsn_old;
    p_st_sess->i_ttc8_stmt_resp_udskpos = i_ttc8_stmt_resp_udskpos_old;
    p_st_sess->i_ttc8_stmt_resp_udsflg = i_ttc8_stmt_resp_udsflg_old;
    return FR_FAIL;
}

/*******************************************************************************
*  FUNC     :  对应于 void unmarshal() 方法
*  ARGS     :  *p_st_conn                           (ct信息)
*           :  *p_st_sess                           (Oracle私有的session)
*           :  *p_st_ora_packet                     (Oracle数据包)
*           :  *p_st_stmt                           (statement)
*           :  i_index                              (当前uds的序号)
*           :  i_numuds                             (总共有多少个uds)
*  RTN      :  0                                    (FR_SUCC)
*              1                                    (FR_FAIL)
*  NOTE     :
*******************************************************************************/
int ora_ttc8_tti_uds_unmarshal(Conntrack *p_st_conn
                            , L7session_ora *p_st_sess
                            , L7_ora_packet *p_st_ora_packet
                            , Sql_statement *p_st_stmt
                            , int i_index
                            , int i_numuds)
{
    ORA_PRINT_INFO("enter uds\n");
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_ora_private *p_st_session_private = (L7session_ora_private *) frct_get_session_private(p_st_session);
    L7_ora_curr_session *p_st_curr_sess = p_st_session_private->p_st_curr_sess;

    short s_tmp = 0;
    short s_udskpos = 0;
    short s_udsflg = 0;
    int i_tmp_len = 0;
    char *p_tmp = NULL;

    int i_ttc8_stmt_resp_udskpos_old = p_st_sess->i_ttc8_stmt_resp_udskpos;
    int i_ttc8_stmt_resp_udsflg_old = p_st_sess->i_ttc8_stmt_resp_udsflg;

    if (ora_ttc8_tti_oac_unmarshal(p_st_conn, p_st_sess, p_st_ora_packet, APM_ORA_OACTYPE_COLUMN, p_st_stmt) != FR_SUCC)
        goto error;

    if (ora_mar_skipUB1(p_st_ora_packet) != FR_SUCC) // i
        goto error;

    if (ora_mar_skipUB1(p_st_ora_packet) != FR_SUCC) // udscnl
        goto error;

    if (ora_mar_unmarshalDALC(p_st_sess, p_st_ora_packet, &p_tmp, &i_tmp_len) == FR_SUCC) // udscolnm
    {
        if (p_tmp != NULL && i_tmp_len == 4 && strncmp(p_tmp, "NULL", 4) == 0)
            p_st_curr_sess->i_null_num++;
#if APM_ORA_DBG
        char *p_udscolnm = NULL;
        unsigned int ui_udscolnm_len = 0;
        if (ora_str_to_utf8(p_st_sess, p_st_sess->s_cli_charset_out, p_tmp, (unsigned int) i_tmp_len, &p_udscolnm, &ui_udscolnm_len) != FR_SUCC)
            goto error;
        ORA_PRINT_ERR("udscolnm:[%s]\n", p_udscolnm);
        ora_free((void **) &p_udscolnm);
#endif
        ora_free((void **) &p_tmp);
    }
    else
        goto error;

    if (ora_mar_skipDALC(p_st_sess, p_st_ora_packet) != FR_SUCC) // udssnm
        goto error;

    if (ora_mar_skipDALC(p_st_sess, p_st_ora_packet) != FR_SUCC) // udstnm
        goto error;

    if ((p_st_sess->i_ttc8_stmt_resp_udskpos == APM_ORA_FLAG_UNSET || p_st_sess->i_ttc8_stmt_resp_udsflg == APM_ORA_FLAG_UNSET) && i_index == 0)
    {
        int i_start_position = bytebuff_getPosition(p_st_ora_packet->p_st_packet);
        if (ora_mar_peekUB1(p_st_ora_packet, &s_udskpos) != FR_SUCC) // udskpos
            goto error;
        if (s_udskpos == 0x00)
        {
            p_st_sess->i_ttc8_stmt_resp_udskpos = FR_YES;
            if (ora_mar_skip(p_st_ora_packet, 1) != FR_SUCC)
                goto error;
            if (ora_mar_peekUB1(p_st_ora_packet, &s_udsflg) != FR_SUCC) // udsflg
                goto error;
            // udsflg位(表示securityAttribute)为0时，表明一定存在udsflg，但是不为0时，不一定存在udsflg
            ORA_PRINT_ALERT("s_udsflg:[%d]\n", s_udsflg);
            if (s_udsflg == 0x00)
                p_st_sess->i_ttc8_stmt_resp_udsflg = FR_YES;
            else if (i_numuds == 1 && s_udsflg <= 4)
            {
                if (ora_mar_peekUB2(p_st_ora_packet, &s_tmp) != FR_SUCC) // dcb部分的 arrayOfByte1
                    goto error;
                if (s_tmp == 0x0107)
                    p_st_sess->i_ttc8_stmt_resp_udsflg = FR_NO;
            }
            else if (i_numuds > 1 && s_udsflg > 4)
                p_st_sess->i_ttc8_stmt_resp_udsflg = FR_NO;
            else if (i_numuds >= APM_ORA_TTC8_UDS_ANALYZE_MORE_BY + 1)
            {
                if (__ora_ttc8_tti_uds_analyze_jdbc(p_st_conn, p_st_sess, p_st_ora_packet, i_index) != FR_SUCC)
                    goto error;
            }
        }
        else
        {
            p_st_sess->i_ttc8_stmt_resp_udskpos = FR_NO;
            p_st_sess->i_ttc8_stmt_resp_udsflg = FR_NO;
        }

        if (bytebuff_setPosition(p_st_ora_packet->p_st_packet, i_start_position) != FR_SUCC)
            goto error;

        ORA_PRINT_ALERT("set i_ttc8_stmt_resp_udskpos = %s, i_ttc8_stmt_resp_udsflg = %s\n",
                        p_st_sess->i_ttc8_stmt_resp_udskpos == APM_ORA_FLAG_UNSET ? "UNSET" : (p_st_sess->i_ttc8_stmt_resp_udskpos == FR_YES ? "FR_YES" : "FR_NO"),
                        p_st_sess->i_ttc8_stmt_resp_udsflg == APM_ORA_FLAG_UNSET ? "UNSET" : (p_st_sess->i_ttc8_stmt_resp_udsflg == FR_YES ? "FR_YES" : "FR_NO"));
    }

    if (p_st_sess->i_use_oci == FR_NO && (p_st_sess->i_ttc8_stmt_resp_udskpos == APM_ORA_FLAG_UNSET || p_st_sess->i_ttc8_stmt_resp_udsflg == APM_ORA_FLAG_UNSET))
        goto error;

    if (p_st_sess->i_ttc8_stmt_resp_udskpos == FR_YES)
    {
        ORA_PRINT_INFO("unmarshal udskpos\n");
        if (ora_mar_skipSB2(p_st_sess, p_st_ora_packet) != FR_SUCC) // udskpos
            goto error;
        if (p_st_sess->i_ttc8_stmt_resp_udsflg == FR_YES)
        {
            ORA_PRINT_INFO("unmarshal udsflg\n");
            if (ora_mar_skipSB4(p_st_sess, p_st_ora_packet) != FR_SUCC) // udsflg
                goto error;
        }
        else if (p_st_sess->i_use_oci == FR_YES && p_st_sess->s_tns_version >= 314 && p_st_sess->c_ttc_version >= 6 && p_st_sess->i_ptr_len == APM_ORA_PTR_LEN_8)
        {
            ORA_PRINT_INFO("unmarshal udsflg\n");
            if (ora_mar_skipSB4(p_st_sess, p_st_ora_packet) != FR_SUCC) // udsflg
                goto error;
        }
    }

    if ((i_ttc8_stmt_resp_udskpos_old == APM_ORA_FLAG_UNSET || i_ttc8_stmt_resp_udsflg_old == APM_ORA_FLAG_UNSET) &&
            p_st_sess->i_ttc8_stmt_resp_udskpos != APM_ORA_FLAG_UNSET && p_st_sess->i_ttc8_stmt_resp_udsflg != APM_ORA_FLAG_UNSET &&
            (p_st_sess->i_ttc8_stmt_resp_dcbkm == APM_ORA_FLAG_UNSET ||
             p_st_sess->i_ttc8_stmt_resp_dcbni1 == APM_ORA_FLAG_UNSET ||
             p_st_sess->i_ttc8_stmt_resp_dcbab2 == APM_ORA_FLAG_UNSET))
    {
        if (p_st_sess->i_ttc8_stmt_resp_udskpos == FR_NO)
        {
            p_st_sess->i_ttc8_stmt_resp_dcbkm = FR_NO;
            p_st_sess->i_ttc8_stmt_resp_dcbni1 = FR_NO;
            p_st_sess->i_ttc8_stmt_resp_dcbab2 = FR_NO;
            p_st_sess->i_ttc8_stmt_resp_oerendseq = FR_NO;
        }
        else
        {
            p_st_sess->i_ttc8_stmt_resp_dcbkm = FR_YES;
            p_st_sess->i_ttc8_stmt_resp_oerendseq = FR_YES;
            if (p_st_sess->i_ttc8_stmt_resp_udsflg == FR_YES)
            {
                p_st_sess->i_ttc8_stmt_resp_dcbni1 = FR_YES;
                p_st_sess->i_ttc8_stmt_resp_dcbab2 = FR_YES;
            }
        }

        ORA_PRINT_ALERT("set i_ttc8_stmt_resp_dcbkm = %s, i_ttc8_stmt_resp_dcbni1 = %s, i_ttc8_stmt_resp_dcbab2 = %s, i_ttc8_stmt_resp_oerendseq = %s\n",
                        p_st_sess->i_ttc8_stmt_resp_dcbkm == APM_ORA_FLAG_UNSET ? "UNSET" : (p_st_sess->i_ttc8_stmt_resp_dcbkm == FR_YES ? "FR_YES" : "FR_NO"),
                        p_st_sess->i_ttc8_stmt_resp_dcbni1 == APM_ORA_FLAG_UNSET ? "UNSET" : (p_st_sess->i_ttc8_stmt_resp_dcbni1 == FR_YES ? "FR_YES" : "FR_NO"),
                        p_st_sess->i_ttc8_stmt_resp_dcbab2 == APM_ORA_FLAG_UNSET ? "UNSET" : (p_st_sess->i_ttc8_stmt_resp_dcbab2 == FR_YES ? "FR_YES" : "FR_NO"),
                        p_st_sess->i_ttc8_stmt_resp_oerendseq == APM_ORA_FLAG_UNSET ? "UNSET" : (p_st_sess->i_ttc8_stmt_resp_oerendseq == FR_YES ? "FR_YES" : "FR_NO"));
    }

    ORA_PRINT_INFO("leave uds\n");
    return FR_SUCC;
error:
    ora_free((void **) &p_tmp);
    p_st_sess->i_ttc8_stmt_resp_udskpos = i_ttc8_stmt_resp_udskpos_old;
    p_st_sess->i_ttc8_stmt_resp_udsflg = i_ttc8_stmt_resp_udsflg_old;
    return FR_FAIL;
}
